<template>
    <!-- 班级开课 -->

    <div class="app-container">
       
        <h4 class="form-header h4">选择班级开课</h4>
        <el-row>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="openSelectClazz"
            >选择班级开课</el-button>
          </el-col>
        </el-row>
        
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
            <el-form-item label="班级名称" prop="name">
            <el-input
                v-model="queryParams.name"
                placeholder="请输入班级名称"
                clearable
                @keyup.enter.native="handleQuery"
            />
            </el-form-item>
            <el-form-item label="班级码" prop="clazzCode">
            <el-input
                v-model="queryParams.clazzCode"
                placeholder="请输入手机号码"
                clearable
                @keyup.enter.native="handleQuery"
            />
            </el-form-item>
            <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row>
            <el-table @row-click="clickRow" ref="table" :data="clazzList" @selection-change="handleSelectionChange" height="260px">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="班级名称" prop="name" :show-overflow-tooltip="true" />
            <el-table-column label="班主任" prop="headTeacher" :show-overflow-tooltip="true" />
            <el-table-column label="班级吗" prop="clazzCode" :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            </el-table>
            <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
            />
        </el-row>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSelectClazz">关闭课程</el-button>
            <el-button @click="visible = false">取 消</el-button>
        </div>
        <select-clazz ref="select" :courseId="queryParams.courseId" @ok="handleQuery" />
    </div>
   
  </template>
  
  <script>
  import { allocatedClazzList, clazzUnSelectAll } from "@/api/feedback/course";
  import selectClazz from "./selectClazz"
  export default {
    dicts: ['sys_normal_disable'],
    components:{selectClazz},
    data() {
      return {
        // 遮罩层
  
        // 选中数组值
        clazzIds: [],
        // 总条数
        total: 0,
        // 未授权用户数据
        clazzList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          courseId: undefined,
          name: undefined,
          clazzCode: undefined
        }
      };
    },
    created() {
          const courseId = this.$route.params && this.$route.params.courseId;
          if (courseId) {
              this.queryParams.courseId = courseId;
              this.handleQuery();
          }
    },
        
    methods: {

        /** 打开待选择班级弹窗 */
      openSelectClazz() {
        this.$refs.select.show();
      },
      // 显示弹框
      clickRow(row) {
        this.$refs.table.toggleRowSelection(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.clazzIds = selection.map(item => item.id);
      },
      // 查询表数据
      getList() {
        allocatedClazzList(this.queryParams).then(res => {
          this.clazzList = res.rows;
          this.total = res.total;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 选择授权用户操作 */
      handleSelectClazz() {
        const courseId = this.queryParams.courseId;
        const clazzIds = this.clazzIds.join(",");
        if (clazzIds == "") {
          this.$modal.msgError("请选择要分配的班级");
          return;
        }
        clazzUnSelectAll({ courseId: courseId, clazzIds: clazzIds }).then(res => {
          this.$modal.msgSuccess(res.msg);
          this.getList();
          if (res.code === 200) {
            this.visible = false;
            this.$emit("ok");
          }
        });
      }
    }
  };
  </script>
  